﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SD.Common;
using SD.IBUS;
using SD.BUS;
using SD.Entities;

namespace SD.UI
{
    public partial class LeadContacts : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                if (!String.IsNullOrEmpty(Request.Params["contactId"]))
                {
                    SelectRowOnLoading();
                }
            }
        }

        protected void btnSearchAddress_Click(object sender, EventArgs e)
        {
            IContactBIZ iContact = new ContactBIZ();
            DataTable dt = iContact.ListContactByCodition(tbxFirstName.Text, tbxSurname.Text);

            if (dt.Rows.Count > 0)
            {
                gvSearchAddress.DataSource = dt;
                gvSearchAddress.DataBind();
                lbMessage.Text = string.Empty;
            }
            else
            {
                dt.Rows.Add(dt.NewRow());
                gvSearchAddress.DataSource = dt;
                gvSearchAddress.DataBind();
                gvSearchAddress.Rows[0].Visible = false;
                lbMessage.Text = "No records found";
            }
            
        }

        protected void btnNone_Click(object sender, EventArgs e)
        {

        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            tbxFirstName.Text = "";
            tbxSurname.Text = "";
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {

        }

        protected void btnClose_Click(object sender, EventArgs e)
        {

        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {

        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            if (gvSearchAddress.SelectedValue == null)
            {
                Response.Write("<script>alert('Please Choose Row need Select')</script>");
                return;
            }
            else
            {
                string selectValue = gvSearchAddress.Rows[gvSearchAddress.SelectedIndex].Cells[2].Text;//Request.Form["rdbChoose"];
                string contactValue = gvSearchAddress.SelectedValue.ToString();//Request.Form["contactID"];            
                string str = "var Str = '" + selectValue + "';var ctID='" + contactValue + "';" +
                    " window.opener.document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_tabsDetail1_tbxLeadContact').value=Str;" +
                    "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_txtContactID').value=ctID;" +
                    "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_hdfContactName').value=Str;window.close();";
                ScriptManager.RegisterClientScriptBlock(btnSelect, typeof(Button), "select", str, true);
            }
        }

        private void SelectRowOnLoading()
        {
            for (Int32 i = 0; i < gvSearchAddress.PageCount; i++)
            {
                gvSearchAddress.PageIndex = i;
                DataBind();

                Boolean isExit = false;
                foreach (GridViewRow row in gvSearchAddress.Rows)
                {
                    String keyValue = gvSearchAddress.DataKeys[row.RowIndex].Value.ToString();

                    if (keyValue.Equals(Request.Params["contactId"]))
                    {
                        gvSearchAddress.SelectedIndex = row.RowIndex;
                        RadioButton rdb = (RadioButton)row.FindControl("rdbChoose");
                        rdb.Checked = true;

                        isExit = true;
                        break;
                    }
                }
                if (isExit)
                {
                    break;
                }
            }
        }

        private DataTable LoadData()
        {
            IContactBIZ iContactBIZ = new ContactBIZ();
            DataTable dt = iContactBIZ.ListActiveContact();
            if (dt.Rows.Count > 0)
            {
                gvSearchAddress.DataSource = dt;
                gvSearchAddress.DataBind();
                lbMessage.Text = string.Empty;
            }
            else
            {
                dt.Rows.Add(dt.NewRow());
                gvSearchAddress.DataSource = dt;
                gvSearchAddress.DataBind();
                gvSearchAddress.Rows[0].Visible = false;
                lbMessage.Text = "No records found";
            }

            return dt;
        }

        protected void chkInActive_CheckedChanged(object sender, EventArgs e)
        {
            IContactBIZ iContact = new ContactBIZ();
            if (chkInActive.Checked)
            {
                DataTable dt = iContact.ListAllContact();

                gvSearchAddress.DataSource = dt;
                gvSearchAddress.DataBind();
            }
            else
            {
                DataTable dt = iContact.ListActiveContact();

                gvSearchAddress.DataSource = dt;
                gvSearchAddress.DataBind();
            }
        }

        protected void gvSearchAddress_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSearchAddress.PageIndex = e.NewPageIndex;
            LoadData();
            gvSearchAddress.SelectedIndex = -1;
            foreach (GridViewRow row in gvSearchAddress.Rows)
            {
                String keyValue = gvSearchAddress.DataKeys[row.RowIndex].Value.ToString();

                if (keyValue.Equals(Request.Params["contactId"]))
                {
                    gvSearchAddress.SelectedIndex = row.RowIndex;
                    RadioButton rdb = (RadioButton)row.FindControl("rdbChoose");
                    rdb.Checked = true;
                }
            }
        }

        protected void rdbChoose_CheckedChanged(object sender, EventArgs e)
        {
            Deselect();
            RadioButton rb = (RadioButton)sender;
            rb.Checked = true;
            GridViewRow row = (GridViewRow)((RadioButton)sender).NamingContainer;
            if (row != null)
            {
                    gvSearchAddress.SelectedIndex = row.RowIndex;
            }                    
        }

        private void Deselect()
        {
            int i=0;
            foreach(GridViewRow r in gvSearchAddress.Rows)
            {
                RadioButton rb = (RadioButton)(gvSearchAddress.Rows[i].FindControl("rdbChoose"));
                rb.Checked = false;
                i++;

            }
        }
    }
}
